package com.ruoyi.task;


import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @Author alison
 * @Date 2024/11/29 19:04
 * @Version 1.0
 * @Description
 */

@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
@Configuration
@SpringBootTest
public class BootQuartzJob implements ApplicationRunner {
    public static void main(String[] args) {
        System.setProperty("spring.main.web-application-type", "none");
//        SpringApplication.run(BootQuartzJob.class, args);
    }

    @Bean
    public Trigger sTrigger() {
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever();
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger-1", "trigger-group-1").withSchedule(simpleScheduleBuilder).forJob(sJobDetail()).build();
        return trigger;
    }

    @Bean
    public JobDetail sJobDetail() {
        return JobBuilder.newJob(SJob.class).withIdentity("job-1", "job-group-1").usingJobData("count", 1).storeDurably().build();
    }

    @Autowired
    private ApplicationContext applicationContext;


    @Autowired
    private Scheduler scheduler;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        JobDetail jobDetail = applicationContext.getBean(JobDetail.class);
        System.out.println("jobDetail = " + jobDetail);
//        scheduler.start();
    }


    @PersistJobDataAfterExecution
    @DisallowConcurrentExecution // 解决 并发执行的问题, 并发执行是 执行时间超过定时时间，
    // 阻止并发执行，下次执行时间是， max(执行时间, 下次执行时间-定时时间)
    public static class SJob implements Job {

        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            Integer count = (Integer) context.getJobDetail().getJobDataMap().get("count");
            System.out.println("count = " + count);
            context.getJobDetail().getJobDataMap().put("count", ++count);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
